package perso.android.savethemage.v1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.ColorModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleAtModifier;
import org.andengine.entity.modifier.PathModifier.IPathModifierListener;
import org.andengine.entity.particle.SpriteParticleSystem;
import org.andengine.entity.particle.emitter.CircleOutlineParticleEmitter;
import org.andengine.entity.particle.emitter.PointParticleEmitter;
import org.andengine.entity.particle.initializer.AlphaParticleInitializer;
import org.andengine.entity.particle.initializer.BlendFunctionParticleInitializer;
import org.andengine.entity.particle.initializer.ColorParticleInitializer;
import org.andengine.entity.particle.initializer.RotationParticleInitializer;
import org.andengine.entity.particle.initializer.ScaleParticleInitializer;
import org.andengine.entity.particle.initializer.VelocityParticleInitializer;
import org.andengine.entity.particle.modifier.AlphaParticleModifier;
import org.andengine.entity.particle.modifier.ColorParticleModifier;
import org.andengine.entity.particle.modifier.ExpireParticleInitializer;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.svg.opengl.texture.atlas.bitmap.SVGBitmapTextureAtlasTextureRegionFactory;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.SurfaceScrollDetector;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleAsyncGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;
import org.andengine.util.modifier.BaseModifier;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.IModifier.IModifierListener;
import org.andengine.util.modifier.ease.EaseSineInOut;
import org.andengine.util.progress.IProgressListener;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.opengl.GLES20;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;


class CameraDim { public static int CAMERA_WIDTH =  480,  CAMERA_HEIGHT = 853; }

public class MenuScrollerActivity extends SimpleAsyncGameActivity {//implements  IOnSceneTouchListener, IClickDetectorListener {
   
        protected static int FONT_SIZE = 24;
        protected static int PADDING = 50;
        
        protected static int MENUITEMS = 6;
        private Scene mScene;
        private Camera mCamera;

        protected Font mFont; 
        protected ITexture mFontTexture;     
    
        private Rectangle scrollBar;        
        private List<TiledTextureRegion> columns = new ArrayList<TiledTextureRegion>();
		private BitmapTextureAtlas mMenuTexture;
		private BitmapTextureAtlas mMenuTexture2;

		private ITextureRegion mParticleTextureRegion;
       
		private MenuScrollerActivity menu = this;
  	  
  	    protected MenuScene mMenuScene;
		private BitmapTextureAtlas mMenuTexture3;
		private BitmapTextureAtlas mMenuTexture4;
		private ITextureRegion mMenuTextureRegionTuto;
		private ITextureRegion mMenuTextureRegionPlay;
		private ITextureRegion mMenuTextureRegionCredits;
		private ITextureRegion mMenuTextureRegionQuit;
  	   
		private int x = 0, y = 0, decalage = 0, decalage2 = 0, sizeStar = 0, sizeMage = 0;
		private BitmapTextureAtlas mBitmapTextureAtlasParticule;
		private BitmapTextureAtlas mTextureStar;
		
		private ITextureRegion mSVGTextureRegionsMage;
		private BuildableBitmapTextureAtlas mBuildableBitmapTextureAtlas;
		
		
		private Sprite credB;
		private Sprite playB;
		private Sprite tutoB;
		private Sprite quiB;
		private ITextureRegion mCircleTexture, mCircleTextureB;
		private BuildableBitmapTextureAtlas mBitmaptextureCircle, mBitmaptextureCircleB;
		
		private int sizeCircle = 0;
		private TiledTextureRegion mFaceTextureRegionMage;
		private BuildableBitmapTextureAtlas mBitmapTextureAtlasMage;
		private int widthItem;
		private int heightItem;
		private BitmapTextureAtlas mMenuTextureFond;
		private ITextureRegion mMenuTextureRegionFond;
		

		public EngineOptions onCreateEngineOptions() {

			
    		DisplayMetrics metrics = new DisplayMetrics();
    		getWindowManager().getDefaultDisplay().getMetrics(metrics);
    		CameraDim.CAMERA_HEIGHT = metrics.heightPixels;
    		CameraDim.CAMERA_WIDTH = metrics.widthPixels;
			this.mCamera = new Camera(0, 0, CameraDim.CAMERA_WIDTH, CameraDim.CAMERA_HEIGHT);
			
			y = (int) (CameraDim.CAMERA_HEIGHT*0.1f);
			x = (int) (CameraDim.CAMERA_WIDTH*0.2f );
			decalage = (int) (CameraDim.CAMERA_WIDTH*0.14f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			decalage2 = (int) (CameraDim.CAMERA_WIDTH*0.1f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			sizeStar = (int) (CameraDim.CAMERA_WIDTH*0.07f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			sizeMage = (int) (CameraDim.CAMERA_WIDTH*0.3f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			sizeCircle = (int) (CameraDim.CAMERA_WIDTH*0.3f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			
			widthItem = (int) (CameraDim.CAMERA_WIDTH*0.3f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			heightItem = (int) (CameraDim.CAMERA_HEIGHT*0.2f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			
			return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CameraDim.CAMERA_WIDTH, CameraDim.CAMERA_HEIGHT), this.mCamera);
		}

		@Override
		public Engine onCreateEngine(final EngineOptions pEngineOptions) {

        	return new Engine(pEngineOptions);
		}


		public void addParticule(float x, float y) {

			final PointParticleEmitter particleEmitter = new PointParticleEmitter(x, y);
			//final CircleOutlineParticleEmitter particleEmitter = new CircleOutlineParticleEmitter(x, y,  0);
			final SpriteParticleSystem particleSystem = new SpriteParticleSystem(particleEmitter, 7, 7, 7, this.mParticleTextureRegion, this.getVertexBufferObjectManager());
			
			particleSystem.setParticlesSpawnEnabled(true);
			//particleSystem.addParticleModifier(pParticleModifier)
			particleSystem.addParticleInitializer(new ColorParticleInitializer<Sprite>(1f, 1f, 1f));
			particleSystem.addParticleInitializer(new ScaleParticleInitializer<Sprite>(0.3f));
			particleSystem.addParticleInitializer(new AlphaParticleInitializer<Sprite>(1));
			particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE));
			particleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(-163, 163, -163, 163));
			particleSystem.addParticleInitializer(new RotationParticleInitializer<Sprite>(0.0f, 360.0f));

			particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(4.5f, 11.5f, 1.0f, 0.5f ));
			mScene.attachChild(particleSystem);


			mScene.registerUpdateHandler(new TimerHandler(8f, new ITimerCallback() {
				public void onTimePassed(final TimerHandler pTimerHandler){
					particleSystem.setParticlesSpawnEnabled(false);
					mScene.detachChild(particleSystem);
					mScene.unregisterUpdateHandler(pTimerHandler);
				}              
			}));

		}
		
		
		public void addParticuleB(float x, float y, int nb) {

			final PointParticleEmitter particleEmitter = new PointParticleEmitter(x, y);
			//final CircleOutlineParticleEmitter particleEmitter = new CircleOutlineParticleEmitter(x, y,  150);
			final SpriteParticleSystem particleSystem = new SpriteParticleSystem(particleEmitter, nb, nb, nb, this.mParticleTextureRegion, this.getVertexBufferObjectManager());
			
			particleSystem.setParticlesSpawnEnabled(true);
			//particleSystem.addParticleModifier(pParticleModifier)
			particleSystem.addParticleInitializer(new ColorParticleInitializer<Sprite>(1f, 1f, 1f));
			particleSystem.addParticleInitializer(new AlphaParticleInitializer<Sprite>(0.8f));
			particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE));
			particleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(20, 65, -185, -90));
			particleSystem.addParticleInitializer(new RotationParticleInitializer<Sprite>(0.0f, 360.0f));
			particleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(180f));

			particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0, 80, 0.3f, 0.1f));
			particleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(4, 20, 1, 1, 1f, 0.8f, 0.8f, 1));
			particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(4.5f, 11.5f, 1.0f, 0.5f ));
		
			mScene.attachChild(particleSystem);


		

		}

		@Override
		public void onCreateResourcesAsync(IProgressListener pProgressListener)
				throws Exception {
			
			BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
			
			this.mMenuTextureFond = new BitmapTextureAtlas(this.getTextureManager(), 2048, 2048, TextureOptions.BILINEAR);
			
			this.mMenuTextureRegionFond = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTextureFond, this, "fond.png", 0, 0);
			this.mMenuTextureFond.load();
			
			pProgressListener.onProgressChanged(10);
		
			
			this.mMenuTexture = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
			this.mMenuTexture2 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
			this.mMenuTexture3 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
			this.mMenuTexture4 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
			
			pProgressListener.onProgressChanged(40);
			
			this.mMenuTextureRegionTuto = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture, this, "tuto.png", 0, 0);
			this.mMenuTextureRegionPlay = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture2, this, "play.png", 0, 0);
			this.mMenuTextureRegionCredits = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture3, this, "credits.png", 0, 0);
			this.mMenuTextureRegionQuit = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture4, this, "quit.png", 0, 0);
			this.mMenuTexture.load();
			this.mMenuTexture2.load();
			this.mMenuTexture3.load();
			this.mMenuTexture4.load();
			
			this.mBitmapTextureAtlasParticule = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.NEAREST);
			this.mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlasParticule, this, "star.png", 0, 0);

			this.mBitmapTextureAtlasParticule.load();
			
			
			this.mBitmapTextureAtlasMage = new BuildableBitmapTextureAtlas(this.getTextureManager(), 2048, 2048, TextureOptions.NEAREST);
			
			this.mFaceTextureRegionMage = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlasMage, this, "Mage_animation.png", 4, 3);

			try {
				this.mBitmapTextureAtlasMage.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
				this.mBitmapTextureAtlasMage.load();
			} catch (TextureAtlasBuilderException e) {
				Debug.e(e);
			}
			
		
			
			pProgressListener.onProgressChanged(100);
		}

		@Override
		public Scene onCreateSceneAsync(IProgressListener pProgressListener)
				throws Exception {
			this.mScene = new Scene();
			
			this.mScene.setBackground(new Background(0.83f, 0.83f, 0.95f));
			this.mScene.attachChild(new Sprite(0, 0, CameraDim.CAMERA_WIDTH, CameraDim.CAMERA_HEIGHT, this.mMenuTextureRegionFond, this.getVertexBufferObjectManager()));
			
			final int duration = 100;
			
			final long durationTab[]  =  {120,120,120,120,120,120,120,120,120};
			
			int size = (int) (CameraDim.CAMERA_WIDTH*0.2f *CameraDim.CAMERA_HEIGHT/CameraDim.CAMERA_WIDTH);
			
			//final TiledTextureRegion tiledTextureRegion = (TiledTextureRegion)this.mSVGTextureRegionsMage;
			float pX = CameraDim.CAMERA_WIDTH*0.03f;
			float pY = CameraDim.CAMERA_HEIGHT - size*1.5f;
			
			final AnimatedSprite animatedSprite = new AnimatedSprite(pX, pY, 
					size, size, mFaceTextureRegionMage, this.getVertexBufferObjectManager());
			
			
			
			this.mScene.attachChild(animatedSprite);
			
		
			
			final float time = 0.6f;
			
			playB = new Sprite(x, y, this.mMenuTextureRegionPlay, this.getVertexBufferObjectManager()){
	            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
	            	
	            	if(pSceneTouchEvent.isActionDown()) {
	             
		            	addParticule(pSceneTouchEvent.getX() - CameraDim.CAMERA_WIDTH*0.08f, pSceneTouchEvent.getY() - CameraDim.CAMERA_HEIGHT*0.08f);

		            	ColorModifier col = new ColorModifier(time, new org.andengine.util.color.Color(1f, 1f, 1f), new org.andengine.util.color.Color(0.4f, 0.4f, 0.4f));
		       		
		            	playB.registerEntityModifier(col); 
		            	
		                MenuScrollerActivity.this.mScene.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback() {
	                           
                            public void onTimePassed(TimerHandler pTimerHandler) {
                            	
                        		Intent  intent = new Intent(menu, GameActivity.class);
                        		startActivity(intent);

                        		playB.setColor(new org.andengine.util.color.Color(1,1,1));
     
                            }
		                }));   
		                
		                animatedSprite.animate(durationTab, 0, 8, false);
		                addParticuleB(animatedSprite.getX() + decalage2, animatedSprite.getY(), 4);   
		                
	            	}
	            	return true;
	            }
	    	};
	    	
	    	tutoB = new Sprite(x, y+decalage, this.mMenuTextureRegionTuto, this.getVertexBufferObjectManager()){
	            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
	             
	            	if(pSceneTouchEvent.isActionDown()) {
		            	addParticule(pSceneTouchEvent.getX() - CameraDim.CAMERA_WIDTH*0.08f, pSceneTouchEvent.getY() - CameraDim.CAMERA_HEIGHT*0.08f);
		            	ColorModifier col = new ColorModifier(time, new org.andengine.util.color.Color(1f, 1f, 1f), new org.andengine.util.color.Color(0.4f, 0.4f, 0.4f));
		            	tutoB.registerEntityModifier(col); 
		            	
		                MenuScrollerActivity.this.mScene.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback() {
	                           
                            public void onTimePassed(TimerHandler pTimerHandler) {

                            	tutoB.setColor(new org.andengine.util.color.Color(1,1,1));
                                  
                            }
		                }));   	
		                
		                
		                animatedSprite.animate(durationTab, 0, 8, false);
		                addParticuleB(animatedSprite.getX() + decalage2, animatedSprite.getY(), 4);   
		                
	            	}

	            	return true;
	            }
	    	};
	    	
	    	credB = new Sprite(x, y+decalage*2, this.mMenuTextureRegionCredits, this.getVertexBufferObjectManager()){
	            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
	            	
	            	if(pSceneTouchEvent.isActionDown()) {
	             
		            	addParticule(pSceneTouchEvent.getX() - CameraDim.CAMERA_WIDTH*0.08f, pSceneTouchEvent.getY() - CameraDim.CAMERA_HEIGHT*0.08f);
		            	ColorModifier col = new ColorModifier(time, new org.andengine.util.color.Color(1f, 1f, 1f), new org.andengine.util.color.Color(0.4f, 0.4f, 0.4f));
		            	credB.registerEntityModifier(col); 
		            	
		                MenuScrollerActivity.this.mScene.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback() {
	                           
                            public void onTimePassed(TimerHandler pTimerHandler) {

                        		credB.setColor(new org.andengine.util.color.Color(1,1,1));
      
                            }
		                }));
		                animatedSprite.animate(durationTab, 0, 8, false);
		                addParticuleB(animatedSprite.getX() + decalage2, animatedSprite.getY(), 4);   
	            	}

	            	return true;
	            }
	    	};
	    	

	        quiB = new Sprite(x, y+decalage*3, this.mMenuTextureRegionQuit, this.getVertexBufferObjectManager()){
	            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
	            	
	            	if(pSceneTouchEvent.isActionDown()) {
	             
		            	addParticule(pSceneTouchEvent.getX() - CameraDim.CAMERA_WIDTH*0.08f , pSceneTouchEvent.getY() - CameraDim.CAMERA_HEIGHT*0.08f);
		            	ColorModifier col = new ColorModifier(time, new org.andengine.util.color.Color(1f, 1f, 1f), new org.andengine.util.color.Color(0.4f, 0.4f, 0.4f)); 
		            	quiB.registerEntityModifier(col); 
	
		                MenuScrollerActivity.this.mScene.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback() {
	                           
                            public void onTimePassed(TimerHandler pTimerHandler) {
                            	
                        		quiB.setColor(new org.andengine.util.color.Color(1,1,1));
                        		MenuScrollerActivity.this.finish();
                                 
                            }
		                }));
		                animatedSprite.animate(durationTab, 0, 8, false);
		                addParticuleB(animatedSprite.getX() + decalage2, animatedSprite.getY(), 4);   
	            	}
	            	return true;
	            }
	    	};
	    	
	    	/*float pScale = 0.8f;
		
	    	
			
			
			tutoB.setScale(pScale );
			playB.setScale(pScale );
			credB.setScale(pScale );
			quiB.setScale(pScale );
			*/
	    	this.mScene.attachChild(tutoB);
	    	this.mScene.attachChild(playB);
	    	this.mScene.attachChild(credB);
	    	this.mScene.attachChild(quiB);
	    	
	    	this.mScene.registerTouchArea(tutoB);
	    	this.mScene.registerTouchArea(playB);
	    	this.mScene.registerTouchArea(credB);
	    	this.mScene.registerTouchArea(quiB);
	    	
	    	//addCircle(240, 240);
	    	
		
            return this.mScene;
		}


		@Override
		public void onPopulateSceneAsync(Scene pScene,
				IProgressListener pProgressListener) throws Exception {
			// TODO Auto-generated method stub
			
		}
}
 